package com.example.demo1.controller.rest;

import com.example.demo1.common.ResponseCode;
import com.example.demo1.common.ServerResponse;
import com.example.demo1.entity.BaUser;
import com.example.demo1.service.impl.service.UserService;
import com.example.demo1.util.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserRestController {

    private static Logger logger = LoggerFactory.getLogger(UserRestController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/qryUsersForPT")
    public ServerResponse qryUsersForPT(String userName,
                                        HttpServletRequest request,
                                        @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){

        logger.info("时间：" + new Date() + "===============调用UserRestController.qryUsersForPT方法");
        return userService.qryUsersForPT(userName, pageNum, pageSize);
    }


    @PostMapping("/addUserInfoForPT")
    public ServerResponse addUserInfoForPT(BaUser user, String roleIds, HttpServletRequest request){

        logger.info("时间：" + new Date() + "===============调用UserRestController.addUserInfoForPT方法");

        Integer userId;

        try {
            Map<String, Object> decodeResult = JwtUtil.dececodeToken(request.getHeader("Authorization"));
            if(!StringUtils.isEmpty(decodeResult.get("companyId")) && !StringUtils.isEmpty(decodeResult.get("userId"))){
                userId = Integer.valueOf(decodeResult.get("userId").toString());
            }else{
                logger.error("时间：" + new Date() + "==================调用UserRestController.addUserInfoForPT方法token解析出错");
                return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
        }

        return userService.addUserInfoForPT(user, userId, roleIds);
    }

    @PostMapping("/addUserInfo")
    public ServerResponse addUserInfo(BaUser user, HttpServletRequest request){

        logger.info("时间：" + new Date() + "===============调用UserRestController.addUserInfo方法");

        Integer userId;

        try {
            Map<String, Object> decodeResult = JwtUtil.dececodeToken(request.getHeader("Authorization"));
            if(StringUtils.isEmpty(decodeResult.get("userId"))){
                userId = Integer.valueOf(decodeResult.get("userId").toString());
            }else{
                logger.error("时间：" + new Date() + "==================调用UserRestController.addUserInfo方法token解析出错");
                return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
        }

        return userService.addUserInfo(user, userId);
    }

    @PostMapping("/editUserInfoForPT")
    public ServerResponse editUserInfoForPT(BaUser user, String roleIds, HttpServletRequest request){

        logger.info("时间：" + new Date() + "===============调用UserRestController.editUserInfoForPT方法");

        Integer companyId, userId;

        try {
            Map<String, Object> decodeResult = JwtUtil.dececodeToken(request.getHeader("Authorization"));
            if(!StringUtils.isEmpty(decodeResult.get("userId"))){
                userId = Integer.valueOf(decodeResult.get("userId").toString());
            }else{
                logger.error("时间：" + new Date() + "==================调用UserRestController.editUserInfoForPT方法token解析出错");
                return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
        }

        return userService.editUserInfoForPT(user, userId, roleIds);
    }

    @PostMapping("/editUserInfo")
    public ServerResponse editUserInfo(BaUser user, HttpServletRequest request){

        logger.info("时间：" + new Date() + "===============调用UserRestController.editUserInfo方法");

        Integer companyId, userId;

        try {
            Map<String, Object> decodeResult = JwtUtil.dececodeToken(request.getHeader("Authorization"));
            if(!StringUtils.isEmpty(decodeResult.get("userId"))){
                userId = Integer.valueOf(decodeResult.get("userId").toString());
            }else{
                logger.error("时间：" + new Date() + "==================调用UserRestController.editUserInfo方法token解析出错");
                return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
        }

        return userService.editUserInfo(user, userId);
    }

    @PostMapping("/deleteUserInfoForPT")
    public ServerResponse deleteUserInfoForPT(String userIds){

        logger.info("时间：" + new Date() + "===============调用UserRestController.deleteUserInfoForPT方法");

        return userService.deleteUserInfoForPT(userIds);
    }

    @PostMapping("/deleteUserInfo")
    public ServerResponse deleteUserInfo(String userIds){

        logger.info("时间：" + new Date() + "===============调用UserRestController.deleteUserInfo方法");

        return userService.deleteUserInfo(userIds);
    }

    @PostMapping("/selectUserRoleByUserId")
    public ServerResponse selectUserRoleByUserId(HttpServletRequest request){

        String userId = "";

        try {
            Map<String, Object> decodeResult = JwtUtil.dececodeToken(request.getHeader("Authorization"));
            if(!StringUtils.isEmpty(decodeResult.get("userId"))){
                userId = decodeResult.get("userId").toString() ;
            }else{
                logger.error("时间：" + new Date() + "==================调用UserRestController.addUserInfo方法token解析出错");
                return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
        }
        return userService.selectUserRoleByUserId(userId) ;
    }


    /**
     * 个人中心-数据接口
     * @param request
     * @return
     */
    @RequestMapping("/getUserCenterInfo")
    public ServerResponse getUserCenterInfo(HttpServletRequest request) {
        Integer userId;
        try {
            userId = Integer.parseInt(String.valueOf(JwtUtil.dececodeToken(request.getHeader("Authorization")).get("userId")));
        } catch (Exception e) {
            logger.error("时间：" + new Date() + "=====================调用SalesController.getUserCenterInfo方法token解析失败", e);
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), "token解析失败");
        }
        return userService.getUserCenterInfo(userId);
    }

    /**
     * 个人中心-修改密码
     * @param request
     * @return
     */
    @RequestMapping("/userCenterUpdatePasswd")
    public ServerResponse userCenterUpdatePasswd(HttpServletRequest request, String originPasswd, String newPasswd) {
        Integer userId;
        try {
            userId = Integer.parseInt(String.valueOf(JwtUtil.dececodeToken(request.getHeader("Authorization")).get("userId")));
        } catch (Exception e) {
            logger.error("时间：" + new Date() + "=====================调用SalesController.userCenterUpdatePasswd方法token解析失败", e);
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), "token解析失败");
        }
        return userService.userCenterUpdatePasswd(userId, originPasswd, newPasswd);
    }

}
